<script setup lang="ts">
	import { getGuessLikeApI } from "@/services/home"
	import type { PageParams } from "@/types/global.d.ts"
	import type { GuessItem } from "@/types/home"
	import { onMounted, ref } from "vue"
	const pageParams : Required<PageParams> = {
		page: 1,
		pageSize: 10
	}
	const guessList = ref<GuessItem[]>([])
	const finish = ref(false)
	const getdata = async () => {
		console.log("getmore")
		if (finish.value === true) {
			return uni.showToast({
				icon: 'none',
				title: "没有更多数据"
			})
		}
		const res = await getGuessLikeApI(pageParams)
		guessList.value.push(...res.result.items);
		if (pageParams.page < res.result.pages) {
			pageParams.page++
		} else {
			finish.value = true
		}
	}
	const resetData = () => {
		pageParams.page = 1
		guessList.value = []
		finish.value = false
	}
	onMounted(() => {
		getdata()
	})
	defineExpose({
		resetData,
		getMore: getdata,
	})
</script>
<template>
	<view class="caption">
		<text class="text">猜你喜欢</text>

	</view>
	<view class="guess">
		<navigator :url="`/pages/goods/goods?id=${item.id}`" class="guess-item" v-for="item in guessList" :key="item.id">
			<image class="image" mode="aspectFill" :src="item.picture"></image>
			<view class="name">
				{{item.name}}
			</view>
			<view class="price">
				<text class="small">￥</text>
				<text>{{item.price}}</text>

			</view>
		</navigator>

	</view>

	<view class="loading-text">
		{{finish?'没有更多数据了':' 正在加载...'}}


	</view>
</template>



<style>
	:host {
		display: block;
	}

	.caption {
		display: flex;
		justify-content: center;
		line-height: 1;
		padding: 36rpx 0 40rpx;
		font-size: 32rpx;
		color: #262626;
	}

	.caption .text {
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 0 28rpx 0 30rpx;
	}

	.guess {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		padding: 0 20rpx;
	}

	.guess-item {
		width: 345rpx;
		padding: 24rpx 20rpx 20rpx;
		margin-bottom: 20rpx;
		border-radius: 10rpx;
		overflow: hidden;
		background-color: #fff;
	}

	.guess .image {
		width: 304rpx;
		height: 304rpx;
	}

	.guess .name {
		height: 75rpx;
		margin: 10rpx 0;
		font-size: 26rpx;
		color: #262626;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}

	.guess .price {
		line-height: 1;
		padding-top: 4rpx;
		color: #cf4444;
		font-size: 26rpx;
	}

	.guess .small {
		font-size: 80%;
	}

	.loading-text {
		text-align: center;
		font-size: 28rpx;
		color: #666;
		padding: 20rpx 0;
	}
</style>